﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

namespace Web_HengliSeed.Sys_Manage
{
    public partial class M_EditBringInfo : System.Web.UI.Page
    {
        protected string ProImage = "../Images/noImage.gif";
        protected void Page_Load(object sender, EventArgs e)
        {
            ClassFile.Function.CheckState();
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            string strPID = Common.GetMes.GetRequestQuery(Request, "PID");
            if (!string.IsNullOrEmpty(strPID))
            {
                OleDbDataReader DataRead = BLL.BringInfo.GetNoteMes(strPID);
                if (DataRead.HasRows)
                {
                    DataRead.Read();
                    txtProName.Value = Server.HtmlDecode(DataRead["T_BringTitle"].ToString());
                    //txtProNote.Value = Server.HtmlDecode(DataRead["T_BringNote"].ToString());
                    HImageName.Value = DataRead["T_BringImage"].ToString();
                    ProImage = "../UpLoad/Image/Small/" + HImageName.Value;

                }
                DataRead.Close();
                DataRead.Dispose();
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            string lan = "cn";
            if (!String.IsNullOrEmpty(Request["lan"]))
                lan = Request["lan"];
            string strPID = Common.GetMes.GetRequestQuery(Request, "PID");
            string strProName = Server.HtmlEncode(txtProName.Value.Trim());
            if (BLL.BringInfo.EditBringMes(strPID, strProName, HImageName.Value, string.Empty, lan, chkShow.Checked))
            {
                Common.ShowMessage.Show(Page, "error", "信息保存成功...", "M_BringInfo.aspx?lan=" + Request["lan"]);
            }
            else
            {
                Common.ShowMessage.Show(Page, "error", "信息保存失败， 请重试或联系管理人员...");
            }
        }

        protected void BtnUpLoad_Click(object sender, EventArgs e)
        {
            HImageName.Value = (HImageName.Value == "noImage.gif") ? Common.GetMes.GetNowTimeString() + UploadImg.FileName.Substring(UploadImg.FileName.LastIndexOf(".")) : HImageName.Value;
            switch (ClassFile.Function.UpFile(Page, UploadImg, "img", HImageName.Value))
            {
                case 1:
                    string newsImageName = HImageName.Value;
                    string smallImageName = HImageName.Value.Substring(0, HImageName.Value.IndexOf("."));
                    string strBakName = HImageName.Value.Substring(HImageName.Value.IndexOf(".") + 1);
                    ClassFile.Function.MakeThumbnail(Server.MapPath("~/UpLoad/image/" + newsImageName), Server.MapPath("~/UpLoad/image/Small/" + smallImageName + "." + strBakName), 180, 150, "Cut", strBakName);
                    ProImage = "../UpLoad/image/" + newsImageName;
                    
                    break;
                case -1:
                    Common.ShowMessage.Show(Page, "e1", "上传文件格式有误，限制使用gif，jpeg。");
                    break;
                case -2:
                    Common.ShowMessage.Show(Page, "e2", "上传文件太大，请确保图片小于500K。");
                    break;
                default:
                    Common.ShowMessage.Show(Page, "e2", "未知错误，请重试或联系管理人员。");
                    break;
            }
        }


    }
}
